package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.container.page.PageHandler;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.common.PageResult;
import com.xrit.sunnyday.common.QueryPageBean;
import com.xrit.sunnyday.dao.OrderSettingMapper;
import com.xrit.sunnyday.pojo.OrderSetting;
import com.xrit.sunnyday.service.OrderSettingService;
import com.xrit.sunnyday.common.SunnydayException;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;
    @Override
    public void add(ArrayList<OrderSetting> list) throws SunnydayException{
        //1,遍历list集合
        for (OrderSetting orderSetting : list) {
            //2,拿到用户传递来的日期数据
            Date orderDate = orderSetting.getOrderDate();
            //3,调用dao层根据该日期查询是否在ordersetting中存在的方法
            OrderSetting ordersettingInDb=orderSettingMapper.findOrderDate(orderDate);
            //4,进行判断,不为空代表存在
            if(ordersettingInDb!=null){
                //6,判断数据库中的已预约人数是否大于用户传递来的可预约人数
                //  如果大于则报错
                if(ordersettingInDb.getReservations()>orderSetting.getNumber()){
                    throw new SunnydayException(orderSetting.getOrderDate()
                            +"无法修改可预约人数,因为已预约的人数比你现在设置的可预约人数要大");
                }
                //小于则可以修改
                //7,调用根据日期修改的方法
                orderSettingMapper.update(orderSetting);
            }else{
                //5,不存在 调用添加方法
                orderSettingMapper.add(orderSetting);
            }

        }
    }

    @Override
    public List<Map<String, Integer>> findByMethod(String method) {
        String startDate = method + "-01";
        return orderSettingMapper.findByMethod(startDate);
    }

    @Override
    public void updateNumberByDate(OrderSetting orderSetting) throws SunnydayException{
        //1,调用根据该日期查询是否在ordersetting中存在的方法
        OrderSetting orderDate = orderSettingMapper.findOrderDate(orderSetting.getOrderDate());
        //2,判断 不等于null代表存在
        if(orderDate!=null){
            //4,再次判断 如果已预约人数比要改的可预约人数要大 则不能修改
            if(orderDate.getReservations()>orderSetting.getNumber()){
                throw new SunnydayException("该日期无法修改可预约人数,因为已预约的人数比你现在设置的可预约人数要大");
            }
            //5,调用根据日期修改可预约人数的方法
            orderSettingMapper.updateNumberByDate(orderSetting);
        }else {
            //3,不存在调用添加方法
            orderSettingMapper.add(orderSetting);
        }


    }

    @Override
    public PageResult findAllPage(QueryPageBean queryPageBean) {
        if(!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<OrderSetting> orderSettingList=orderSettingMapper.findAllPage(queryPageBean.getQueryString());
        PageInfo<OrderSetting> PageInfo = new PageInfo<>(orderSettingList);
        return new PageResult(PageInfo.getTotal(),PageInfo.getList());
    }
}
